comment about last commit
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 */
6
7 /**
8 *
9 */
10 require_once ( 'Feed.php' );
11
12 /**
13 * This is a class for doing query pages; since they're almost all the same,
14 * we factor out some of the functionality into a superclass, and let
15 * subclasses derive from it.
16 *
17 * @package MediaWiki
18 */
19 class QueryPage {
20
21 /**
22 * Subclasses return their name here. Make sure the name is also
23 * specified in SpecialPage.php and in Language.php as a language message
24 * param.
25 */
26 function getName() {
27 return '';
28 }
29
30 /**
31 * Subclasses return an SQL query here.
32 *
33 * Note that the query itself should return the following four columns:
34 * 'type' (your special page's name), 'namespace', 'title', and 'value'
35 * *in that order*. 'value' is used for sorting.
36 *
37 * These may be stored in the querycache table for expensive queries,
38 * and that cached data will be returned sometimes, so the presence of
39 * extra fields can't be relied upon. The cached 'value' column will be
40 * an integer; non-numeric values are useful only for sorting the initial
41 * query.
42 *
43 * Don't include an ORDER or LIMIT clause, this will be added.
44 */
45 function getSQL() {
46 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
47 }
48
49 /**
50 * Override to sort by increasing values
51 */
52 function sortDescending() {
53 return true;
54 }
55
56 function getOrder() {
57 return ' ORDER BY value ' .
58 ($this->sortDescending() ? 'DESC' : '');
59 }
60
61 /**
62 * Is this query expensive (for some definition of expensive)? Then we
63 * don't let it run in miser mode. $wgDisableQueryPages causes all query
64 * pages to be declared expensive. Some query pages are always expensive.
65 */
66 function isExpensive( ) {
67 global $wgDisableQueryPages;
68 return $wgDisableQueryPages;
69 }
70
71 /**
72 * Sometime we dont want to build rss / atom feeds.
73 */
74 function isSyndicated() {
75 return true;
76 }
77
78 /**
79 * Formats the results of the query for display. The skin is the current
80 * skin; you can use it for making links. The result is a single row of
81 * result data. You should be able to grab SQL results off of it.
82 * If the function return "false", the line output will be skipped.
83 */
84 function formatResult( $skin, $result ) {
85 return '';
86 }
87
88 /**
89 * The content returned by this function will be output before any result
90 */
91 function getPageHeader( ) {
92 return '';
93 }
94
95 /**
96 * This is the actual workhorse. It does everything needed to make a
97 * real, honest-to-gosh query page.
98 *
99 * @param $offset database query offset
100 * @param $limit database query limit
101 */
102 function doQuery( $offset, $limit ) {
103 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
104 global $wgMiserMode;
105
106 $sname = $this->getName();
107 $fname = get_class($this) . '::doQuery';
108 $sql = $this->getSQL();
109 $dbr =& wfGetDB( DB_SLAVE );
110 $dbw =& wfGetDB( DB_MASTER );
111 $querycache = $dbr->tableName( 'querycache' );
112
113 $wgOut->setSyndicated( $this->isSyndicated() );
114 $res = false;
115
116 if ( $this->isExpensive() ) {
117 $recache = $wgRequest->getBool( 'recache' );
118 if( $recache ) {
119 # Clear out any old cached data
120 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
121
122 # Do query on the (possibly out of date) slave server
123 $maxstored = 1000;
124 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
125
126 # Fetch results
127 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
128 $first = true;
129 while ( $row = $dbr->fetchObject( $res ) ) {
130 if ( $first ) {
131 $first = false;
132 } else {
133 $insertSql .= ',';
134 }
135 $insertSql .= '(' .
136 $dbw->addQuotes( $row->type ) . ',' .
137 $dbw->addQuotes( $row->namespace ) . ',' .
138 $dbw->addQuotes( $row->title ) . ',' .
139 $dbw->addQuotes( $row->value ) . ')';
140 }
141
142 # Save results into the querycache table on the master
143 $dbw->query( $insertSql, $fname );
144
145 # Set result pointer to allow reading for display
146 $numRows = $dbr->numRows( $res );
147 if ( $numRows <= $offset ) {
148 $num = 0;
149 } else {
150 $dbr->dataSeek( $res, $offset );
151 $num = max( $limit, $numRows - $offset );
152 }
153 }
154 if( $wgMiserMode || $recache ) {
155 $type = $dbr->strencode( $sname );
156 $sql =
157 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
158 FROM $querycache WHERE qc_type='$type'";
159 }
160 if( $wgMiserMode ) {
161 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
162 }
163 }
164 if ( $res === false ) {
165 $res = $dbr->query( $sql . $this->getOrder() .
166 $dbr->limitResult( $limit,$offset ), $fname );
167 $num = $dbr->numRows($res);
168 }
169
170
171 $sk = $wgUser->getSkin( );
172
173 $wgOut->addHTML( $this->getPageHeader() );
174
175 $top = wfShowingResults( $offset, $num);
176 $wgOut->addHTML( "<p>{$top}\n" );
177
178 # often disable 'next' link when we reach the end
179 if($num < $limit) { $atend = true; } else { $atend = false; }
180
181 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
182 $wgOut->addHTML( "<br />{$sl}</p>\n" );
183
184 if ( $num > 0 ) {
185 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
186 # Only read at most $num rows, because $res may contain the whole 1000
187 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
188 $format = $this->formatResult( $sk, $obj );
189 if ( $format ) {
190 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
191 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
192 $s .= "<li{$attr}>{$format}</li>\n";
193 }
194 }
195 $dbr->freeResult( $res );
196 $s .= '</ol>';
197 $wgOut->addHTML( $s );
198 }
199 $wgOut->addHTML( "<p>{$sl}</p>\n" );
200 }
201
202 /**
203 * Similar to above, but packaging in a syndicated feed instead of a web page
204 */
205 function doFeed( $class = '' ) {
206 global $wgFeedClasses;
207 global $wgOut, $wgLanguageCode, $wgLang;
208 if( isset($wgFeedClasses[$class]) ) {
209 $feed = new $wgFeedClasses[$class](
210 $this->feedTitle(),
211 $this->feedDesc(),
212 $this->feedUrl() );
213 $feed->outHeader();
214
215 $dbr =& wfGetDB( DB_SLAVE );
216 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
217 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
218 while( $obj = $dbr->fetchObject( $res ) ) {
219 $item = $this->feedResult( $obj );
220 if( $item ) $feed->outItem( $item );
221 }
222 $dbr->freeResult( $res );
223
224 $feed->outFooter();
225 return true;
226 } else {
227 return false;
228 }
229 }
230
231 /**
232 * Override for custom handling. If the titles/links are ok, just do
233 * feedItemDesc()
234 */
235 function feedResult( $row ) {
236 if( !isset( $row->title ) ) {
237 return NULL;
238 }
239 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
240 if( $title ) {
241 if( isset( $row->timestamp ) ) {
242 $date = $row->timestamp;
243 } else {
244 $date = '';
245 }
246
247 $comments = '';
248 if( $title ) {
249 $talkpage = $title->getTalkPage();
250 $comments = $talkpage->getFullURL();
251 }
252
253 return new FeedItem(
254 $title->getText(),
255 $this->feedItemDesc( $row ),
256 $title->getFullURL(),
257 $date,
258 $this->feedItemAuthor( $row ),
259 $comments);
260 } else {
261 return NULL;
262 }
263 }
264
265 function feedItemDesc( $row ) {
266 $text = '';
267 if( isset( $row->comment ) ) {
268 $text = htmlspecialchars( $row->comment );
269 } else {
270 $text = '';
271 }
272
273 if( isset( $row->text ) ) {
274 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
275 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
276 }
277 return $text;
278 }
279
280 function feedItemAuthor( $row ) {
281 if( isset( $row->user_text ) ) {
282 return $row->user_text;
283 } else {
284 return '';
285 }
286 }
287
288 function feedTitle() {
289 global $wgLanguageCode, $wgSitename, $wgLang;
290 $page = SpecialPage::getPage( $this->getName() );
291 $desc = $page->getDescription();
292 return "$wgSitename - $desc [$wgLanguageCode]";
293 }
294
295 function feedDesc() {
296 return wfMsg( 'tagline' );
297 }
298
299 function feedUrl() {
300 global $wgLang;
301 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
302 return $title->getFullURL();
303 }
304 }
305
306 /**
307 * This is a subclass for very simple queries that are just looking for page
308 * titles that match some criteria. It formats each result item as a link to
309 * that page.
310 *
311 * @package MediaWiki
312 */
313 class PageQueryPage extends QueryPage {
314
315 function formatResult( $skin, $result ) {
316 global $wgContLang;
317 $nt = Title::makeTitle( $result->namespace, $result->title );
318 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );
319 }
320 }
321
322 ?>